package com.yl.applicationsecurity.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-01-26 11:52:30
 */




public class AgentCheck {

    // 获取代理列表
    public static List<String> getAgentList() throws IOException {
        String url = "http://10.33.155.29:8080/api/clientStatus/page?status=1";
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0");
        connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
        connection.setRequestProperty("Connection", "close");
        connection.setRequestProperty("Upgrade-Insecure-Requests", "1");
        connection.setRequestProperty("Priority", "u=0, i");

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // 使用 fastjson 解析响应数据
        JSONArray body = JSONArray.parseArray(response.toString());
        List<String> agentList = new ArrayList<>();
        for (int i = 0; i < body.size(); i++) {
            JSONObject item = body.getJSONObject(i);
            agentList.add(item.getString("appName"));
        }
        return agentList;
    }



    public static void main(String[] args) throws IOException {
        // 获取代理列表
        List<String> agentList = getAgentList();

        System.out.println(agentList);


    }
}
